﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using XMLGener.Utils;
using XMLGener.Data;
using XMLGener.interfaces;

namespace XMLGener.GUI.components
{
    /// <summary>
    /// 
    /// </summary>
    public partial class XModuleEditor : UserControl,IXModuleField
    {
        /// <summary>
        /// Constuctor
        /// </summary>
        public XModuleEditor()
        {
            InitializeComponent();
        }
       
        /// <summary>
        /// Source node
        /// </summary>
        //private XMLGenerNode sourceNode = null;

        //private XModuleDefinitinoInstance sourceMod = null;

        private List<IXModuleField> fields = new List<IXModuleField>();

        string modDefinition = null;

        private int componentIndex = 0;
        /// <summary>
        /// Create components by module definition.
        /// </summary>
        /// <param name="sourceMod"></param>
        public void createComponentsByModule(XModuleDefinitionInstance sourceMod)
        {
            this.modDefinition = sourceMod.getDefinition();
            initializeByDefinition();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sourceMod"></param>
        public void createComponentsByModule(XModuleOutputInstance sourceMod)
        {
            this.modDefinition = sourceMod.getDefinition();
            initializeByDefinition();

        }

        private void initializeByDefinition()
        {
            //this.sourceMod = sourceMod;
            //if (this.sourceMod == null)
             //   return;
            if (modDefinition == null)
                return;
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(modDefinition);
            XmlElement attrs = XMLUtils.getSubElement(xdoc.DocumentElement, "attributes");
            if (attrs == null) 
                return;
            XmlElement attr = XMLUtils.getSubElement(attrs, "attribute");
            Stack<XmlElement> stack = new Stack<XmlElement>();
            while (attr != null)
            {
                stack.Push(attr);
                attr = XMLUtils.getNextElement(attr, "attribute");
            }
            componentIndex = 0;
            while (stack.Count>0)
            {
                attr = stack.Pop();
                XModuleParam par = XModuleParam.instanceFromXML(attr);
                IXModuleField newField = null;
                switch (par.type)
                {
                    case "text":
                        newField = addNewTextItem(par);
                        break;
                    case "number":
                        newField = addNewNumItem(par);
                        break;
                    case "float":
                        newField = addNewFloatItem(par);
                        break;
                    case "password":
                        newField = addNewPasswordItem(par);
                        break;
                    case "generatedFile":
                        newField = addNewGeneratedFileItem(par);
                        break;
                    case "file":
                        newField = addNewFileSelectorItem(par);
                        break;
                    case "select":
                        newField = addNewSelectItem(par);
                        break;
                    case "bool":
                        newField = addNewBooleanItem(par);
                        break;
                    case "info":
                        newField = addNewInfoItem(par);
                        break;
                }
                if (newField != null)
                {
                    fields.Add(newField);
                }
                
                //attr = XMLUtils.getNextElement(attr, "attribute");
            }
        }

        private IXModuleField addNewTextItem(XModuleParam param)
        { 
            XTextItem newTextItem = new XTextItem(false);
            setAndAddControlParams(newTextItem);
            newTextItem.setAttr(param);
            return newTextItem;
        }

        
        private IXModuleField addNewNumItem(XModuleParam param)
        {
            XNumberItem newNumItem = new XNumberItem();
            setAndAddControlParams(newNumItem);
            newNumItem.setAttr(param);
            return newNumItem;
        }
        private IXModuleField addNewFloatItem(XModuleParam param)
        {
            XNumberItem newNumItem = new XNumberItem();
            newNumItem.setDecimalPlaces(5);
            setAndAddControlParams(newNumItem);
            newNumItem.setAttr(param);
            return newNumItem;
        }

        private IXModuleField addNewPasswordItem(XModuleParam param)
        {
            XTextItem newTextItem = new XTextItem(true);
            setAndAddControlParams(newTextItem);
            newTextItem.setAttr(param);
            return newTextItem;
        }


        private IXModuleField addNewFileSelectorItem(XModuleParam param)
        {
            XFileSelector newItem = new XFileSelector();
            setAndAddControlParams(newItem);
            newItem.setAttr(param);
            return newItem;
        }

        private IXModuleField addNewGeneratedFileItem(XModuleParam param)
        {
            XGeneratedFileSelector newItem = new XGeneratedFileSelector();
            setAndAddControlParams(newItem);
            newItem.setAttr(param);
            return newItem;
        }

        private IXModuleField addNewSelectItem(XModuleParam param)
        {
            XComboItem newItem = new XComboItem();
            setAndAddControlParams(newItem);
            newItem.setAttr(param);
            return newItem;
        }

        private IXModuleField addNewBooleanItem(XModuleParam param)
        {
            XBooleanItem newItem = new XBooleanItem();
            setAndAddControlParams(newItem);
            newItem.setAttr(param);
            return newItem;
        }

        private IXModuleField addNewInfoItem(XModuleParam param)
        {
            XInfoItem newItem = new XInfoItem();
            setAndAddControlParams(newItem);
            newItem.setAttr(param);
            return newItem;
        }
        
        private void setAndAddControlParams(UserControl component)
        {
            component.Dock = System.Windows.Forms.DockStyle.Top;
            //component.Location = new System.Drawing.Point(0, 0);
            component.Size = new System.Drawing.Size(308, 23);
            component.TabIndex = componentIndex++;
            
            this.Controls.Add(component);
        }

        #region IXModuleField Members

        /// <summary>
        /// Set node to module editor
        /// </summary>
        /// <param name="node"></param>
        public void setXNode(XMLGenerNode node)
        {
            foreach (IXModuleField field in this.fields)
            {
                field.setXNode(node);
            }
        }

        #endregion
    }
}
